<?php
	/**
	 * Logger class
	 *
	 */
	class Logger
	{
		/**
		 * debug array
		 */
		static $debug;
		
		/**
		 * debug array iteration
		 */
		static $iteration = 0;
		
		/**
		 * defines whether to export logger info to a spreadsheet file
		 */
		static $export_to_spreadsheet = false;

		/***
		 * defines whether to export logger info to a text file
		 */
		static $export_to_text = false;
		
		/**
		 * Sets debug log
		 *
		 * @param string $text text to be logged
		 */
		static function Debug($text)
		{
//		    Debugger::Log($text); return;
		    
			global $object;			

			//exits the logger if we do not use system logger
			if ((isset($object->config->use_system_logger)) && ($object->config->use_system_logger))
			{
				//gets the current 
				$current_mktime = microtime() + time();
				$current_time = date("H:i:s");
				
				if (self::$iteration == 0) $previous_mktime = $current_mktime; 
				else $previous_mktime = self::$debug[self::$iteration - 1]['mktime'];
				
				list($decsec, $sec) = explode(" ", $current_mktime);
				$current_full_time = $current_time.':'.round($decsec*1000);
				
				//difference between 2 logs
				$mktime_diff = $current_mktime - $previous_mktime;
				
				//total time
				if (self::$iteration == 0) $total_mktime = 0;
				else $total_mktime = $current_mktime - self::$debug[0]['mktime'];
				
				//debug information
		 		$backtrace = debug_backtrace();
		 		$debug_file = $backtrace[0]['file'];
		 		$debug_line = $backtrace[0]['line'];
		 		$debug_function = $backtrace[1]['function'];
		 		$debug_class = $backtrace[1]['class'];
				
				//insert the element into debug array
				self::$debug[self::$iteration] = array
					(
						'text' => $text, 
						'mktime' => $current_mktime, 
						'time' => $current_full_time, 
						'diff_to_prev' => $mktime_diff, 
						'total_time' => $total_mktime,
						'file' => $debug_file,
						'line' => $debug_line,
						'function' => $debug_function,
						'class' => $debug_class,
					);
				self::$iteration ++;
			}
		}
		
		/**
		 * Generates logger table
		 */
		static function LoggerTable()
		{				
			self::ExportLogger();
			
			echo "<br /><br />";
			echo "<div style = 'float: left; width: 100%;'><table cellpadding = '0' cellspacing = '0' width = '100%' style = 'width: 100%; border-style: solid; border-color: gray; border-width: 0px; background-color: gray; padding: 0px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px;'>";
			
				echo "<tr style = 'color: #3E4755; font-weight: bold; background-color: #E6E7D7; padding-right: 3px; margin-right: 3px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px;'>";
					echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>Time</td>";
					echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>Debug note</td>";
					echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>Difference to previous</td>";
					echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>Total time</td>";
					echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>Information</td>";
					echo "</tr>";
						
				foreach(self::$debug as $elem)
				{
					$elem[diff_to_prev] = round($elem[diff_to_prev]*1000, 2);
					$elem[total_time] = round($elem[total_time]*1000, 2);
					echo "<tr style = 'font-family: verdana; font-size: 11px; color: #3E4755; background-color: #F9F9EF;'>";
						echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>{$elem[time]}</td>";
						echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>{$elem[text]}</td>";
						echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>{$elem[diff_to_prev]}ms</td>";
						echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>{$elem[total_time]}ms</td>";
						echo "<td style = 'font-family: verdana; font-size: 11px; border-collapse:collapse; border-width: 1px; border-style:solid; border-color: gray; padding:0px 0px 0px 3px;'>";
							echo "<b>File:</b> {$elem[file]}; <br /><b>Function:</b> {$elem['function']}; <br /><b>Class:</b> {$elem['class']}; <br /><b>Line:</b> {$elem[line]}"; 
						echo "</td>";
					echo "</tr>";
				}
			echo "</table></div>";
		}

		/**
		 * Exports logger to files
		 */
		static function ExportLogger()
		{
			if (self::$export_to_spreadsheet)
			{
				$xls = new SpreadsheetExport();
				$xls->addRow(Array("Time", "Debug note", "Difference to previous", "Total time", "File", "Function", "Class", "Line"));
				
				foreach (self::$debug as $elem)
				{
					$xls->addRow(Array("{$elem[time]}", "{$elem[text]}", "{$elem[diff_to_prev]}", "{$elem[total_time]}", "{$elem[file]}", "{$elem['function']}", "{$elem['class']}", "{$elem[line]}"));
				}
				
				$xls->CreateFile(LOGS.SEPARATOR.'system_logger.xls');
			}
			
			if (self::$export_to_text)
			{
				$txt = new TextExport();
				
				foreach (self::$debug as $elem)
				{
					$txt->addRow("{$elem[time]}: '{$elem[text]}' took {$elem[diff_to_prev]}ms in {$elem['class']}->{$elem['function']} ({$elem[file]}) on line {$elem[line]}. TOTAL: {$elem[total_time]}ms \r\n");
				}
				
				$txt->CreateFile(LOGS.SEPARATOR.'system_logger.txt');
			}
		}
	}
?>